package com.listen.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateFormatUtil {
	// 返回时间字符串：2017/3/29
	public static String getDateStr(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	
	public static String getDateAnyMinuteBeforeStr(int minutes) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();  
		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - minutes);     
		return df.format(calendar.getTime());
	}
	
	public static String getDateAnyDayAfterStr(int days) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();  
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + days);     
		return df.format(calendar.getTime());
	}
	
	public static Date getDateAnyDayBefore(int days) {
		Calendar calendar = Calendar.getInstance();  
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - days);     
		return calendar.getTime();
	}
	
	public static Date getDateByStr(String date) {
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Date d = null;
		try {
			d = df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static String getDateStr(Date date,String format) {
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}
	
	public static String getDateAnyMinuteBeforeFoer8(int minutes) {
		DateFormat df = new SimpleDateFormat("yyyy/M/d HH:mm:ss");
		Calendar calendar = Calendar.getInstance();  
		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - minutes);     
		return df.format(calendar.getTime());
	}
	
	public static Date getDateAnyMinuteAfter(Date date,int minutes) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + minutes);     
		return calendar.getTime();
	}
	
	
	/** 
	 * 比较两个日期之间的大小 
	 *  
	 * @param d1 
	 * @param d2 
	 * @return 前者大于后者返回true 反之false 
	 */  
	public static boolean compareDate(Date d1, Date d2) {  
	    Calendar c1 = Calendar.getInstance();  
	    Calendar c2 = Calendar.getInstance();  
	    c1.setTime(d1);  
	    c2.setTime(d2);  
	  
	    int result = c1.compareTo(c2);  
	    if (result >= 0)  
	        return true;  
	    else  
	        return false;  
	}
	
	/**
	 * 返回分钟
	 * @param date
	 * @return
	 */
	public static int getMinute(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}
	
	/**
	 * 返回小时
	 * @param date
	 * @return
	 */
	public static int getHour(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}
	
	// 返回时间字符串：2017/3/29
	public static String getDateStr() {
		DateFormat df = new SimpleDateFormat("yyyy/M/dd HH:mm:ss");
		return df.format(new Date());
	}
	
	public static String getDateEN(Date date) {
		DateFormat sdf = new SimpleDateFormat("EE MMM dd yyyy", Locale.US);
		String s = sdf.format(date);
		return SpecialCharacterHandler.replaceBlankSpace(s);
	}
	
	public static String getHexStr() {
		StringBuilder sb = new StringBuilder();
		for(int k = 0;k < 16; k++) {
			int n = (int) (1+Math.random()*(10-1));
			sb.append(n);
		}
		
		Long m = Long.parseLong(sb.toString());
		System.out.println(m);
		String mHex = Long.toHexString(m);
		return mHex;
	}
	 
	
	public static void main(String args[]) throws ParseException {
		for (int i = 0; i < 100; i++) {
			System.out.println((int)(30+Math.random()*(45-30+1)));
		}
//		String str = getDateAnyMinuteBeforeStr(10);
//		SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
//		Date  date=formatter.parse(str);
//		System.out.println(getDateAnyMinuteAfter(date,45));
	}
}
